var execline = 0;
var execlineNum = 6;
$(function () {
    $("#username").blur(function () {
        var username = $(this).val();
        if (username == '') {
            $("#umsg").removeClass().addClass("reg_ts_cw").text("长度6~12个字符，数字或字母组成，必须以字母开头。");
        }
        var re = /\W/g;
        if (username.length > 12 || username.length < 6 || re.test(username) || username.indexOf('_') != -1 || !isNaN(username.charAt(0)))
        {
            $("#umsg").removeClass().addClass("reg_ts_cw");
            return;
        } else {
            /**
             * ajax去后台验证用户名是否已经注册
             */
            $.ajax({
                type: 'get',
                url: domain + '/index.php?s=user/check',
                data: {'username': username},
                dataType: 'json',
                success: function (data) {
                    if (data['flag']) {
                        $("#umsg").removeClass().addClass("reg_ts").text(data['msg']);
                        execlineSet(0);
                    } else {
                        $("#umsg").removeClass().addClass("reg_ts_cw").text(data['msg']);
                        execlineClear(0);
                        return;
                    }
                }
            });
        }
    });


    $("#password").blur(function () {
        var password = $(this).val();
        if (password == '') {
            $("#upass").removeClass().addClass("reg_ts_cw");
            return;
        }
        var reg = /^[A-Za-z0-9]+$/;
        if (password.length > 12 || password.length < 6)
        {
            $("#upass").removeClass().addClass("reg_ts_cw").text("6~12个字符，只允许数字、字母、下划线");
            execlineClear(1);
            return;
        } else {
            $("#upass").removeClass().addClass("reg_ts").text("密码可以使用");
            execlineSet(1);
        }
    });

    $("#repassword").blur(function () {
        if ($("#password").val() != $(this).val()) {
            $("#repass").removeClass().addClass("reg_ts_cw").text("两次输入不一样");
            execlineClear(2);
            return;
        } else {
            $("#repass").removeClass().addClass("reg_ts").text("密码可以使用");
            execlineSet(2);
        }
    });

    $("#realname").blur(function () {
        var ret = chkRealname($('#realname').val());
        if (ret !== true)
        {
            $("#realnameMsg").removeClass().addClass("reg_ts_cw").text(ret);
            execlineClear(3);
            return;
        } else {
            $("#realnameMsg").removeClass().addClass("reg_ts").text("您输入的姓名可用！");
            execlineSet(3);
        }
    });

    $("#idCard").blur(function () {
        var ret = chkIdCard($('#idCard').val());
        if (ret !== true)
        {
            $("#idCardMsg").removeClass().addClass("reg_ts_cw").text(ret);
            execlineClear(4);
            return;
        } else {
            $("#idCardMsg").removeClass().addClass("reg_ts").text("您输入的身份证号可用！");
            execlineSet(4);
        }
    });

    $("#captcha").click(function () {
        $(this).val("");
        execlineClear(5);
    });
    $("#captcha").blur(function () {
        if ($(this).val() != '') {
            execlineSet(5);
        }
    });

    $("#register").click(function () {
        if (execline == (1 << execlineNum) - 1) {
            var username = $("#username").val();
            var password = $("#password").val();
            var repassword = $("#repassword").val();
            var realname = $("#realname").val();
            var idCard = $("#idCard").val();
            var captcha = $("#captcha").val();

            var param = "s=user/register&username=" + username
                    + "&password=" + password
                    + "&repassword=" + repassword
                    + "&captcha=" + captcha
                    + "&realname=" + realname
                    + "&idCard=" + idCard;
            try {
                $.ajax({
                    type: 'get',
                    url: domain + '/index.php?',
                    data: param,
                    dataType: "json",
                    success: function (data) {
                        if (data['ret'] == 0) {
                            alert(data['msg']);
                            window.location.href=domain;
                        } else {
                            show_msg(data['msg']);
                        }
                    },
                    error: function (data) {
                        show_msg("网络忙，请稍后在试！");
                    }
                });
            } catch (e) {

            }
        } else {
            show_msg("信息填写不完整！");
        }
    });

});


function chkRealname(a) {
    return /^[\u4e00-\u9fa5]+$/.test(a) ? !0 : "请输入真实姓名，如 张三"
}

function execlineSet(num) {
    execline = execline | (1 << num);
}

function execlineClear(num) {
    var binNum = (1 << num);
    if ((execline & binNum) === binNum) {
        execline -= binNum;
    }
}

function chkIdCard(a) {
    var b = "身份证号无效，如 440106198202020555";
    if (!a)
        return "请输入身份证号，如 440106198202020555";
    if (!/^[0-9xX]+$/.test(a))
        return b;
    if (15 !== a.length && 18 !== a.length)
        return b;
    var c, d, e, f, g, h, i = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    },
    j = a.split("");
    if (null == i[parseInt(a.substr(0, 2), 10)])
        return b;
    switch (a.length) {
        case 15:
            e = 0 === (parseInt(a.substr(6, 2), 10) + 1900) % 4 || 0 === (parseInt(a.substr(6, 2), 10) + 1900) % 100 && 0 == (parseInt(a.substr(6, 2), 10) + 1900) % 4 ? /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/ : /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/, e.test(a) || (h = b);
            break;
        case 18:
            e = 0 === parseInt(a.substr(6, 4), 10) % 4 || 0 === parseInt(a.substr(6, 4), 10) % 100 && 0 === parseInt(a.substr(6, 4), 10) % 4 ? /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/ : /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/, e.test(a) ? (f = 7 * (parseInt(j[0], 10) + parseInt(j[10], 10)) + 9 * (parseInt(j[1], 10) + parseInt(j[11], 10)) + 10 * (parseInt(j[2], 10) + parseInt(j[12], 10)) + 5 * (parseInt(j[3], 10) + parseInt(j[13], 10)) + 8 * (parseInt(j[4], 10) + parseInt(j[14], 10)) + 4 * (parseInt(j[5], 10) + parseInt(j[15], 10)) + 2 * (parseInt(j[6], 10) + parseInt(j[16], 10)) + parseInt(j[7], 10) + 6 * parseInt(j[8], 10) + 3 * parseInt(j[9], 10), c = f % 11, g = "F", d = "10X98765432", g = d.substr(c, 1), g != j[17].toUpperCase() && (h = b)) : h = b
    }
    return h || !0
}

function show_msg(msg) {
    typeof console !== 'undefined' && console.info(execline);
    alert(msg);
    return false;
}